angular.module('imsApp').controller('channelsController', ['$scope', '$rootScope', '$state', 'ChannelsManager','ImsDialog','ImsAlert',"$sce", function ($scope, $rootScope, $state, ChannelsManager,ImsDialog,ImsAlert,$sce) {
    $scope.$sce=$sce;
    $scope.search = {pageNo:$state.params.pageNo || 1,pageSize:$state.params.pageSize||10,keyword:$state.params.keyword,status:$state.params.status};
    $scope.keyword=$scope.search.keyword;
    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)

        }
    };


    $scope.searchClick=function(keyword){
        $scope.search.pageNo=1;
        $scope.search.keyword=keyword;
        $state.go($state.current.name, $scope.search)
    }
    ChannelsManager.channelsList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
        $scope.channels = result.data;
        $scope.paginationConf.totalItems = result.data.total;

    })

    $scope.status = {1: $scope.table[1], 2:  $scope.table[2]};


    $scope.searchChange=function () {
        $scope.search.pageNo=1;
        $state.go($state.current.name, $scope.search)
    }

    $scope.deletes = function (key) {
        $scope.search.pageNo=1;
        $scope.search[key]=null;
        $state.go($state.current.name, $scope.search)

    }
    
    $scope.deleteChannels=function () {
        var ids=[];
        $scope.channels.pageList.forEach(function (channel) {
            if(channel.checked){
                ids.push(channel.channelId)
            }
        })
        if(!ids.length){
            return false;
        }else{
            ImsDialog.open({title: "删除确认", message: "您确定要删除数据吗?"}).then(function (a) {
                if (a == 'ok') {
                    ChannelsManager.channelsDelete(ids).then(function (result) {
                        $state.reload();
                    })
                }
            })
        }
    }
    
    
    $scope.resetChannelPassword=function (id,name) {
        ChannelsManager.channelsReset(id,{userName:name}).then(function (result) {
           if(result.data.c=='s'){
               ImsAlert.open({message:name+"密码重置成功"})
           }

        })
        
    }

    $scope.changeChannelStatus=function (id,disabled) {
        var status="";
        if(disabled==1){
            status=2;
        }else{
            status=1;
        }
        ChannelsManager.channelsStatusEdit(id,{status:status}).then(function (result) {
            if(result.data.c=="s"){
                $state.reload();
            }

        })
    }
    

    $scope.changeStatus=function(item){
        var status="";
        if(item.status==3){
            status=4;
        }else{
            status=3;
        }
        CampaignsManager.campaignsStatusEdit(item.campaignId,{status:status}).then(function(result){
            $state.reload();
        })
    }
}])

